<template>
  <div class="save_address">
    <my-header>
      <template #left>
        <van-icon name="arrow-left" @click="$router.back()" />
      </template>
      <template #center> 新增地址 </template>
    </my-header>
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="save"
    />
  </div>
</template>

<script>
import { ref, reactive, toRefs } from "vue";
import myHeader from "../../components/myHeader.vue";
import { areaList } from '@vant/area-data';
import { addAddress } from '../../api/secendPage/index';
import { Notify } from "vant";
import { useRouter } from 'vue-router';
export default {
  components: { myHeader },
  setup() {
    const { push } = useRouter();
    const searchResult = ref([
         {
            name: '黄龙万科中心',
            address: '杭州市西湖区',
          },
    ])

    const save = (val) => {
        // console.log(val.areaCode.slice(0,4));
        let obj = {
            token: localStorage.getItem('token'),
            address: val.addressDetail,
            cityId:  val.areaCode.slice(0,2) + '0000',
            code: val.postalCode,
            isDefault: val.isDefault,
            linkMan: val.name,
            mobile: val.tel,
            provinceId: val.areaCode.slice(0,4) + '00',
        }
        // console.log(obj);
      addAddress(obj).then(res => {
         if (res.code == 0){
            Notify({ type: "success", message: "地址保存成功！" });
            push('/site');
         }
      })
    }
    return{
      areaList,
      searchResult,
      save
    }
  },
};
</script>
<style lang="scss" scoped>
.save_address {
  height: 100%;
}
</style>